package com.bardsoftware.foronuvolo.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.safehtml.shared.SafeHtml;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class DiscussionTag {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long myID;
	
	
	@NotPersistent
	public static final DiscussionTag RSS_TAG = new DiscussionTag("RSS");
	
	@Persistent
	private String myTagName;

	//@Persistent
	//private List<Discussion> myDiscussions = new ArrayList<Discussion>();

	public DiscussionTag(String tagName) {
		myTagName = tagName;
	}
	
	//public void addDiscussion(Discussion discussion){
		//myDiscussions.add(discussion);
	//}

	public static DiscussionTag findByName(String name) {
		PersistenceManager pm = PMF.getFactory().getPersistenceManager();
        try {
        	/*
            Query tagQuery = pm.newQuery(DiscussionTag.class);
            tagQuery.setRange(0, 1);
            tagQuery.setFilter("sourceForgeID == paramID");
            tagQuery.declareParameters("Long paramID");
            List<DiscussionTag> result = (List<DiscussionTag>) tagQuery.execute(name);
            */
        	String query = "select from " + DiscussionTag.class.getName() + " where myTagName == '"+name+"'";
            List<DiscussionTag> result = (List<DiscussionTag>) pm.newQuery(query).execute();
            return result.isEmpty() ? null : pm.detachCopy(result.get(0));
        }
        finally {
            pm.close();
        }
	}

	public void save() {
		boolean result = PMF.runInTransaction(this, new Runnable() {
			public void run() {
			}
		}, 3);
		if (!result) {
			//LOGGER.severe("Failed to save tag=" + myID + " tagName="+ myTagName);
		}
	}

	public static Collection<DiscussionTag> getAllTags() {
		PersistenceManager pm = PMF.getFactory().getPersistenceManager();
        try {
        	String query = "select from " + DiscussionTag.class.getName() + " order by myTagName";
            List<DiscussionTag> result = (List<DiscussionTag>) pm.newQuery(query).execute();
            return result.isEmpty() ? null : pm.detachCopyAll(result);
        }
        finally {
            pm.close();
        }
	}

	public String getName() {
		return myTagName;
	}

	public static DiscussionTag getFirstTag() {
		PersistenceManager pm = PMF.getFactory().getPersistenceManager();
        try {
        	String query = "select from " + DiscussionTag.class.getName() + " order by myTagName";
            List<DiscussionTag> result = (List<DiscussionTag>) pm.newQuery(query).execute();
            return result.isEmpty() ? null : pm.detachCopy(result.get(0));
        }
        finally {
            pm.close();
        }
	}

}
